<template>
  <div class="app-container">
    <div class="bg-container">
      <el-tabs v-model="queryParams.priority" @tab-change="getList">
        <el-tab-pane label="高风险" name="高风险"/>
        <el-tab-pane label="低风险" name="低风险"/>
      </el-tabs>
      <el-form v-show="showSearch" ref="queryRef" :inline="true" :model="queryParams">
        <el-form-item label="标记问题的关键词" prop="tags">
          <el-input
              v-model="queryParams.tags"
              clearable
              placeholder="请输入标记问题的关键词"
              @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="问题类型" prop="questionType">
          <el-input
              v-model="queryParams.questionType"
              clearable
              placeholder="请输入问题类型"
              @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="问题状态" prop="status">
          <el-input
              v-model="queryParams.status"
              clearable
              placeholder="请输入问题状态"
              @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button icon="Search" type="primary" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
              :disabled="multiple"
              icon="Delete"
              plain
              type="danger"
              @click="handleDeleteList"
          >删除
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
              icon="Download"
              plain
              type="warning"
              @click="handleExport"
          >导出
          </el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="questionList" @selection-change="handleSelectionChange">
        <el-table-column align="center" type="selection" width="55"/>
        <el-table-column align="center" fixed="left" label="问题表象" prop="appearance"/>
        <el-table-column label="问题状态" prop="status">
          <template #default="scope">
            <el-tag v-if="scope.row.status == 0" type="success">已解决</el-tag>
            <el-tag v-if="scope.row.status == 1" type="danger">未解决</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="问题类型" prop="questionType"/>
        <el-table-column label="聚合的报警数量" prop="warnNumber"/>
        <el-table-column label="问题处理的优先级" prop="priority"/>
        <el-table-column label="更新时间" prop="updateTime">
          <template #default="scope">
            {{ parseTime(scope.row.updateTime) }}
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
          v-model:current-page="queryParams.pageNo"
          v-model:page-size="queryParams.pageSize"
          :page-sizes="[3, 5, 7, 10]"
          :size="10"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          style="margin-top: 20px; justify-content: flex-end"
          @size-change="getList"
          @current-change="getList"
      />
    </div>

    <!-- 添加或修改问题信息对话框 -->
    <el-dialog v-model="open" :title="title" append-to-body width="600px">
      <el-form ref="questionRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="问题表象" prop="appearance">
          <el-input v-model="form.appearance" placeholder="请输入问题表象"/>
        </el-form-item>
        <el-form-item label="问题状态" prop="status">
          <el-input v-model="form.status" placeholder="请输入问题状态"/>
        </el-form-item>
        <el-form-item label="问题类型" prop="questionType">
          <el-input v-model="form.questionType" placeholder="请输入问题类型"/>
        </el-form-item>
        <el-form-item label="聚合的报警数量" prop="warnNumber">
          <el-input v-model="form.warnNumber" placeholder="请输入聚合的报警数量"/>
        </el-form-item>
        <el-form-item label="问题处理的优先级" prop="priority">
          <el-input v-model="form.priority" placeholder="请输入问题处理的优先级"/>
        </el-form-item>
        <el-form-item label="标记问题的关键词" prop="tags">
          <el-input v-model="form.tags" placeholder="请输入标记问题的关键词"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>

<script name="AlertInfo" setup>
import {getCurrentInstance, reactive, ref, toRefs} from "vue";
import {
  addQuestion,
  delQuestion,
  delQuestionList,
  exportQuestion,
  getQuestion,
  listQuestion,
  updateQuestion
} from "@/api/question";
import {parseTime} from "@/utils/utils";

const {proxy} = getCurrentInstance();

const questionList = ref([]);
const open = ref(false);
const loading = ref(false);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNo: 1,
    pageSize: 10,
    appearance: null,
    status: null,
    questionType: null,
    warnNumber: null,
    priority: '高风险',
    tags: null,
  },
  rules: {
    appearance: [
      {required: true, message: "问题表象不能为空", trigger: ["blur", "change"]}
    ],
    status: [
      {required: true, message: "问题状态不能为空", trigger: ["blur", "change"]}
    ],
    questionType: [
      {required: true, message: "问题类型不能为空", trigger: ["blur", "change"]}
    ],
    warnNumber: [
      {required: true, message: "聚合的报警数量不能为空", trigger: ["blur", "change"]}
    ],
    priority: [
      {required: true, message: "问题处理的优先级不能为空", trigger: ["blur", "change"]}
    ],
    tags: [
      {required: true, message: "标记问题的关键词不能为空", trigger: ["blur", "change"]}
    ],
  }
});

const {queryParams, form, rules} = toRefs(data);

// 查询问题信息列表
function getList() {
  loading.value = true;
  listQuestion(queryParams.value).then(response => {
    questionList.value = response.data.list;
    total.value = response.data.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    appearance: null,
    status: null,
    questionType: null,
    warnNumber: null,
    priority: null,
    tags: null,
    creator: null,
    createTime: null,
    updater: null,
    updateTime: null,
  };
  proxy.resetForm("questionRef");
}

// 搜索按钮操作
function handleQuery() {
  queryParams.value.pageNo = 1;
  getList();
}

// 重置按钮操作
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

// 新增按钮操作
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加问题信息";
}

// 修改按钮操作
function handleUpdate(row) {
  reset();
  const id = row.id || ids.value[0]
  getQuestion({id: id}).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改问题信息";
  });
}

// 提交按钮
function submitForm() {
  proxy.$refs["questionRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateQuestion(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addQuestion(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

// 删除按钮操作
function handleDelete(row) {
  proxy.$modal.confirm('是否确认删除问题"' + row.id + '"的数据项？').then(function () {
    return delQuestion({id: row.id});
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

// 批量删除按钮操作
function handleDeleteList() {
  const idList = ids.value;
  proxy.$modal.confirm('是否确认删除问题"' + idList + '"的数据项？').then(function () {
    return delQuestionList({ids: idList.join(',')});
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

// 导出按钮操作
function handleExport() {
  exportQuestion(queryParams.value)
}

getList();
</script>

<style lang="scss" scoped>
</style>